<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\User
 *
 * @property int $id
 * @property string $name
 * @property string $email
 * @property \Illuminate\Support\Carbon|null $email_verified_at
 * @property string $password
 * @property string|null $remember_token
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereEmailVerifiedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\User whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    //用户的文章
    public function posts1(){
        return $this->hasMany(Post::class,'user_id','id')->orderByDesc('created_at')->take(10)->get();
    }
    public function posts(){
        return $this->hasMany(Post::class,'user_id','id');
    }
    //关注我的fan 模型
    public function fans(){
        return $this->hasMany(Fan::class,'star_id','id');
    }
    //我关注的fan 模型
    public function stars(){
        return $this->hasMany(Fan::class,'fan_id','id');
    }
    public function stars1($uid){
        return $this->hasMany(Fan::class,'fan_id','id')->where('star_id',$uid);
    }
    //关注某人

    public function doFan($uid)
    {
        $fans = new Fan();
        $fans->star_id = $uid;

//        $fans = new Fan([
//           'star_id' => $uid
//        ]);
//        return $this->stars()->create(['star_id' => $uid]);
        return $this->stars()->save($fans);
    }
    //取消关注
    public function doUnfan($uid){
        $fan = new \App\Fan();
        $fan->star_id = $uid;
        //delete from `fans` where `fans`.`fan_id` = 102 and `fans`.`fan_id` is not null
        return $this->stars()->delete($fan);
    }
    public function doUnfan1($uid){
        $fan = new \App\Fan();
        $fan->star_id = $uid;
        //delete from `fans` where `fans`.`fan_id` = 102 and `fans`.`fan_id` is not null and `fan_id` = 101
        return $this->stars1($uid)->delete();
    }
    //当前用户是否被UID关注
    public function hasFan($uid){
        return $this->fans()->where('fan_id',$uid)->count();
       // return $this->fans()->where('fan_id',$uid)->exists();

        //return $this->hasOne(\App\Zan::class,'posd_id','id')->where('user_id', $user_id)->exists();

    }
    //当前用户是否关注了UID
    public function hasStar($uid){
        return $this->stars()->where('star_id',$uid)->count();
    }

}
